
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>qiankun · 《前端学习笔记》</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="XiaoChen">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-ancre-navigation/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../../Team/Web前端开发规范.html" />
    
    
    <link rel="prev" href="../../Vue3/Vue3探索.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">前言</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Android</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../../Android/Android 多包名打包应用.html">
            
                <a href="../../Android/Android 多包名打包应用.html">
            
                    
                    Android 多包名打包应用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../../Android/Android UI美化之 shape的使用及其属性总结.html">
            
                <a href="../../Android/Android UI美化之 shape的使用及其属性总结.html">
            
                    
                    Android UI美化之 shape的使用及其属性总结
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">ReactNative</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../../ReactNative/CodePush 热更新配置及使用.html">
            
                <a href="../../ReactNative/CodePush 热更新配置及使用.html">
            
                    
                    CodePush 热更新配置及使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../../ReactNative/已有Android工程 集成React Native.html">
            
                <a href="../../ReactNative/已有Android工程 集成React Native.html">
            
                    
                    已有Android工程 集成React Native
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Egret</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../../Egret/Egret 白鹭引擎参考资料.html">
            
                <a href="../../Egret/Egret 白鹭引擎参考资料.html">
            
                    
                    Egret 白鹭引擎参考资料
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../../Egret/EGER PRO.html">
            
                <a href="../../Egret/EGER PRO.html">
            
                    
                    EGER PRO
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../../Egret/Egret 寻路.html">
            
                <a href="../../Egret/Egret 寻路.html">
            
                    
                    Egret 寻路
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">工具</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../../Tools/homebrew 使用.html">
            
                <a href="../../Tools/homebrew 使用.html">
            
                    
                    homebrew 使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../../Tools/git 使用.html">
            
                <a href="../../Tools/git 使用.html">
            
                    
                    git 使用
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">小程序</li>
        
        
    
        <li class="chapter " data-level="6.1" data-path="../../MiniApp/Atom 完美支持 WePY 小程序开发.html">
            
                <a href="../../MiniApp/Atom 完美支持 WePY 小程序开发.html">
            
                    
                    Atom 完美支持 WePY 小程序开发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="../../MiniApp/Atom 插件安装慢解决方案.html">
            
                <a href="../../MiniApp/Atom 插件安装慢解决方案.html">
            
                    
                    Atom 插件安装慢解决方案
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Vue</li>
        
        
    
        <li class="chapter " data-level="7.1" data-path="../../Vue2/Web 前端环境搭建.html">
            
                <a href="../../Vue2/Web 前端环境搭建.html">
            
                    
                    Web 前端环境搭建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="../../Vue2/Vue 项目搭建.html">
            
                <a href="../../Vue2/Vue 项目搭建.html">
            
                    
                    Vue 项目搭建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="../../Vue2/初识 VueCli 3.html">
            
                <a href="../../Vue2/初识 VueCli 3.html">
            
                    
                    初识 VueCli 3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="../../Vue2/使用 VueCli 3.html">
            
                <a href="../../Vue2/使用 VueCli 3.html">
            
                    
                    使用 VueCli 3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="../../Vue2/VueCli 3 项目配置.html">
            
                <a href="../../Vue2/VueCli 3 项目配置.html">
            
                    
                    VueCli 3 项目配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.6" data-path="../../Vue3/Vue3探索.html">
            
                <a href="../../Vue3/Vue3探索.html">
            
                    
                    初识 Vue3
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">微前端</li>
        
        
    
        <li class="chapter active" data-level="8.1" data-path="./">
            
                <a href="./">
            
                    
                    qiankun
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">团队成长</li>
        
        
    
        <li class="chapter " data-level="9.1" data-path="../../Team/Web前端开发规范.html">
            
                <a href="../../Team/Web前端开发规范.html">
            
                    
                    开发规范
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="../../Team/Web团队建设--npm私库搭建.html">
            
                <a href="../../Team/Web团队建设--npm私库搭建.html">
            
                    
                    私库搭建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.3" data-path="../../Team/Web团队建设--自定义脚手架.html">
            
                <a href="../../Team/Web团队建设--自定义脚手架.html">
            
                    
                    脚手架搭建
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >qiankun</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-anchor"></i><ul><li><span class="title-icon "></span><a href="#&#x5FAE;&#x524D;&#x7AEF;"><b></b>&#x5FAE;&#x524D;&#x7AEF;</a></li><ul><li><span class="title-icon "></span><a href="#&#x7B80;&#x4ECB;"><b></b>&#x7B80;&#x4ECB;</a></li><li><span class="title-icon "></span><a href="#&#x5FAE;&#x524D;&#x7AEF;&#x4F18;&#x52BF;"><b></b>&#x5FAE;&#x524D;&#x7AEF;&#x4F18;&#x52BF;</a></li><li><span class="title-icon "></span><a href="#&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;"><b></b>&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;</a></li><li><span class="title-icon "></span><a href="#qiankun-&#x4F7F;&#x7528;"><b></b>qiankun &#x4F7F;&#x7528;</a></li><ul><li><span class="title-icon "></span><a href="#&#x4E3B;&#x5E94;&#x7528;&#x642D;&#x5EFA;"><b></b>&#x4E3B;&#x5E94;&#x7528;&#x642D;&#x5EFA;</a></li><li><span class="title-icon "></span><a href="#&#x5B50;&#x5E94;&#x7528;&#x642D;&#x5EFA;&#xFF08;vite&#xFF09;"><b></b>&#x5B50;&#x5E94;&#x7528;&#x642D;&#x5EFA;&#xFF08;vite&#xFF09;</a></li></ul></ul></ul></div><a href="#&#x5FAE;&#x524D;&#x7AEF;" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 id="&#x5FAE;&#x524D;&#x7AEF;"><a name="&#x5FAE;&#x524D;&#x7AEF;" class="anchor-navigation-ex-anchor" href="#&#x5FAE;&#x524D;&#x7AEF;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5FAE;&#x524D;&#x7AEF;</h1>
<h2 id="&#x7B80;&#x4ECB;"><a name="&#x7B80;&#x4ECB;" class="anchor-navigation-ex-anchor" href="#&#x7B80;&#x4ECB;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x7B80;&#x4ECB;</h2>
<p>&#x5FAE;&#x524D;&#x7AEF;&#x662F;&#x4E00;&#x79CD;&#x591A;&#x4E2A;&#x56E2;&#x961F;&#x901A;&#x8FC7;&#x72EC;&#x7ACB;&#x53D1;&#x5E03;&#x529F;&#x80FD;&#x7684;&#x65B9;&#x5F0F;&#x6765;&#x5171;&#x540C;&#x6784;&#x5EFA;&#x73B0;&#x4EE3;&#x5316; web &#x5E94;&#x7528;&#x7684;&#x6280;&#x672F;&#x624B;&#x6BB5;&#x53CA;&#x65B9;&#x6CD5;&#x7B56;&#x7565;&#x3002;&#x5FAE;&#x524D;&#x7AEF;&#x7684;&#x6982;&#x5FF5;&#x501F;&#x9274;&#x81EA;&#x540E;&#x7AEF;&#x7684;&#x5FAE;&#x670D;&#x52A1;&#xFF0C;&#x4E3B;&#x8981;&#x662F;&#x4E3A;&#x4E86;&#x89E3;&#x51B3;&#x5927;&#x578B;&#x5DE5;&#x7A0B;&#x5728;&#x53D8;&#x66F4;&#x3001;&#x7EF4;&#x62A4;&#x3001;&#x6269;&#x5C55;&#x7B49;&#x65B9;&#x9762;&#x7684;&#x56F0;&#x96BE;&#x95EE;&#x9898;&#x3002;</p>
<h2 id="&#x5FAE;&#x524D;&#x7AEF;&#x4F18;&#x52BF;"><a name="&#x5FAE;&#x524D;&#x7AEF;&#x4F18;&#x52BF;" class="anchor-navigation-ex-anchor" href="#&#x5FAE;&#x524D;&#x7AEF;&#x4F18;&#x52BF;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5FAE;&#x524D;&#x7AEF;&#x4F18;&#x52BF;</h2>
<ul>
<li>&#x6280;&#x672F;&#x6808;&#x6280;&#x672F;&#x6808;&#x65E0;&#x5173;&#xFF0C;&#x5373;&#x5B50;&#x5E94;&#x7528;&#x53EF;&#x81EA;&#x4E3B;&#x9009;&#x62E9;&#x6280;&#x672F;&#x6846;&#x67B6;&#x5982; <code>Vue2</code>&#x3001;<code>Vue3</code>&#x3001;<code>React</code>&#x3001;<code>Svelte</code>&#x3001;<code>JQuery</code>&#x7B49;</li>
<li>&#x5404;&#x4E3B;&#x5B50;&#x5E94;&#x7528;&#x72EC;&#x7ACB;&#x5F00;&#x53D1;&#x3001;&#x72EC;&#x7ACB;&#x90E8;&#x7F72;&#xFF0C;&#x4EE3;&#x7801;&#x5E93;&#x66F4;&#x5C0F;&#x3001;&#x5185;&#x805A;&#x6027;&#x66F4;&#x5F3A;</li>
<li>&#x53EF;&#x7EF4;&#x62A4;&#x6027;&#x548C;&#x6269;&#x5C55;&#x6027;&#x66F4;&#x597D;&#xFF0C;&#x4FBF;&#x4E8E;&#x5C40;&#x90E8;&#x5347;&#x7EA7;&#x548C;&#x589E;&#x91CF;&#x5347;&#x7EA7;&#xFF0C;&#x5982;&#x5BF9;&#x4E8E;&#x5DE8;&#x578B;&#x5E94;&#x7528;&#x8FDB;&#x884C;&#x6280;&#x672F;&#x91CD;&#x6784;&#x5347;&#x7EA7;&#x53EF;&#x901A;&#x8FC7;&#x5FAE;&#x524D;&#x7AEF;&#x5B9E;&#x73B0;&#x6E10;&#x8FDB;&#x5F0F;&#x91CD;&#x6784;</li>
<li>&#x72EC;&#x7ACB;&#x8FD0;&#x884C;&#x65F6;&#xFF0C;&#x6BCF;&#x4E2A;&#x5FAE;&#x5E94;&#x7528;&#x4E4B;&#x95F4;&#x72B6;&#x6001;&#x9694;&#x79BB;&#xFF0C;&#x8FD0;&#x884C;&#x65F6;&#x72B6;&#x6001;&#x4E0D;&#x5171;&#x4EAB;</li>
</ul>
<h2 id="&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;"><a name="&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;" class="anchor-navigation-ex-anchor" href="#&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;</h2>
<ul>
<li>iframe&#xFF1A;&#x786C;&#x9694;&#x79BB;&#xFF0C;&#x4E0A;&#x4E0B;&#x6587;&#x4E0D;&#x5171;&#x4EAB;&#xFF0C;&#x5F00;&#x53D1;&#x4F53;&#x68C0;&#x8F83;&#x5DEE;</li>
<li>&#x57FA;&#x5EA7;&#x6A21;&#x5F0F;&#xFF1A;<code>qiankun</code> &#x548C; <code>single-spa</code> &#x6A21;&#x5F0F;&#xFF0C;&#x4E3B;&#x5E94;&#x7528;&#x63D0;&#x4F9B;&#x57FA;&#x5EA7;&#xFF0C;&#x5B50;&#x5E94;&#x7528;&#x901A;&#x8FC7;&#x8DEF;&#x7531;&#x8F6C;&#x53D1;&#x3002;</li>
<li>&#x7EC4;&#x5408;&#x5F0F;&#x96C6;&#x6210;&#xFF1A;<code>npm</code> &#x5305;&#x5F62;&#x5F0F;&#x3002;</li>
<li>Web Components</li>
</ul>
<h2 id="qiankun-&#x4F7F;&#x7528;"><a name="qiankun-&#x4F7F;&#x7528;" class="anchor-navigation-ex-anchor" href="#qiankun-&#x4F7F;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a>qiankun &#x4F7F;&#x7528;</h2>
<h3 id="&#x4E3B;&#x5E94;&#x7528;&#x642D;&#x5EFA;"><a name="&#x4E3B;&#x5E94;&#x7528;&#x642D;&#x5EFA;" class="anchor-navigation-ex-anchor" href="#&#x4E3B;&#x5E94;&#x7528;&#x642D;&#x5EFA;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E3B;&#x5E94;&#x7528;&#x642D;&#x5EFA;</h3>
<h4 id="&#x5B89;&#x88C5;-qiankun"><a name="&#x5B89;&#x88C5;-qiankun" class="anchor-navigation-ex-anchor" href="#&#x5B89;&#x88C5;-qiankun"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5B89;&#x88C5; qiankun</h4>
<pre><code class="lang-bash">$ npm i qiankun --save
</code></pre>
<h4 id="&#x914D;&#x7F6E;"><a name="&#x914D;&#x7F6E;" class="anchor-navigation-ex-anchor" href="#&#x914D;&#x7F6E;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x914D;&#x7F6E;</h4>
<p>&#x65B0;&#x5EFA; <code>src/microApps/index.js</code> &#x6587;&#x4EF6;&#xFF0C;&#x5E76;&#x5199;&#x5165;&#x5982;&#x4E0B;&#x5185;&#x5BB9;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { addGlobalUncaughtErrorHandler, runAfterFirstMounted, initGlobalState, start } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;qiankun&apos;</span>;
<span class="hljs-keyword">import</span> Layout <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;@/layout&apos;</span>;
<span class="hljs-keyword">import</span> { getToken } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../utils/auth&apos;</span>;
<span class="hljs-comment">// &#x914D;&#x7F6E;&#x5B50;&#x5E94;&#x7528;&#x52A0;&#x8F7D;&#x76D1;&#x542C;&#x5668;</span>
runAfterFirstMounted(() =&gt; {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;dfcg-micro-front-main ### &#x5B50;&#x5E94;&#x7528;&#x52A0;&#x8F7D;&#x5B8C;&#x6BD5;&apos;</span>);
});
<span class="hljs-comment">// &#x914D;&#x7F6E;&#x5168;&#x5C40;&#x5F02;&#x5E38;&#x6355;&#x83B7;&#x5668;</span>
addGlobalUncaughtErrorHandler((event) =&gt; {
    <span class="hljs-keyword">const</span> { message } = event;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;dfcg-micro-front-main ### addGlobalUncaughtErrorHandler -- &apos;</span>, event);
    <span class="hljs-comment">// &#x52A0;&#x8F7D;&#x5931;&#x8D25;&#x65F6;&#x63D0;&#x793A;</span>
    <span class="hljs-keyword">if</span> (message &amp;&amp; message.includes(<span class="hljs-string">&apos;died in status LOADING_SOURCE_CODE&apos;</span>)) {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5FAE;&#x5E94;&#x7528;&#x52A0;&#x8F7D;&#x5931;&#x8D25;_&apos;</span> + message);
    }
});
<span class="hljs-comment">// &#x4E3B;&#x5B50;&#x5E94;&#x7528;&#x95F4;&#x6570;&#x636E;&#x4F20;&#x9012;</span>
<span class="hljs-comment">// initGlobalState(state) state -- &#x6570;&#x636E;&#x4EA4;&#x4E92;&#x5BF9;&#x8C61;</span>
<span class="hljs-comment">// &#x6CE8;&#xFF1A;</span>
<span class="hljs-comment">//   1. &#x4E3B;&#x5B50;&#x5E94;&#x7528;&#x95F4;&#x6570;&#x636E;&#x5171;&#x4EAB;&#x503C;&#x9700;&#x5728;&#x6B64;&#x5904;&#x5B9A;&#x4E49;</span>
<span class="hljs-comment">//   2. &#x4E3B;&#x5E94;&#x7528;&#x4E2D;&#x672A;&#x5B9A;&#x4E49;&#x7684;&#x53D8;&#x91CF;&#x65E0;&#x6CD5;&#x76D1;&#x542C;&#x6539;&#x53D8;&#xFF0C;&#x540C;&#x65F6;&#x65E0;&#x6CD5;&#x8FDB;&#x884C;&#x6570;&#x636E;&#x4F20;&#x9012;</span>
<span class="hljs-keyword">const</span> actions = initGlobalState({
    token: getToken(),
    test: <span class="hljs-string">&apos;1&apos;</span>,
    test1: {},
});
<span class="hljs-comment">// &#x4E3B;&#x5B50;&#x5E94;&#x7528;&#x95F4;&#x6570;&#x636E;&#x4F20;&#x9012; -- &#x6570;&#x636E;&#x6539;&#x53D8;&#x56DE;&#x8C03;</span>
<span class="hljs-comment">// &#x4E3B;&#x5E94;&#x7528;&#x66F4;&#x65B0;&#x503C; actions.setGlobalState(state)</span>
actions.onGlobalStateChange((state, prev) =&gt; {
    <span class="hljs-comment">// state: &#x53D8;&#x66F4;&#x540E;&#x7684;&#x72B6;&#x6001;; prev &#x53D8;&#x66F4;&#x524D;&#x7684;&#x72B6;&#x6001;</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x4E3B;&#x5E94;&#x7528; ### onGlobalStateChange&apos;</span>, state, prev);
});
<span class="hljs-comment">// &#x542F;&#x52A8; qiankun</span>
start({
    prefetch: <span class="hljs-literal">false</span>, <span class="hljs-comment">// &#x5F00;&#x542F;&#x9884;&#x52A0;&#x8F7D;</span>
    sandbox: {
        <span class="hljs-comment">// strictStyleIsolation: true, // &#x4E25;&#x683C;&#x6837;&#x5F0F;&#x9694;&#x79BB;</span>
        experimentalStyleIsolation: <span class="hljs-literal">true</span>,
    }
});
<span class="hljs-comment">// &#x5FAE;&#x5E94;&#x7528;&#x914D;&#x7F6E;&#x5BF9;&#x8C61;</span>
<span class="hljs-comment">// appName: &#x5FAE;&#x5E94;&#x7528;&#x6807;&#x9898;</span>
<span class="hljs-comment">// name: &#x5FAE;&#x5E94;&#x7528;&#x540D;&#x79F0;&#x6807;&#x8BC6;&#xFF08;&#x5168;&#x5C40;&#x552F;&#x4E00;&#xFF09;</span>
<span class="hljs-comment">// entry: &#x5FAE;&#x5E94;&#x7528;&#x5165;&#x53E3;&#x6807;&#x8BC6;&#xFF08;&#x5168;&#x5C40;&#x552F;&#x4E00;&#xFF09;</span>
<span class="hljs-comment">// container: &#x5FAE;&#x5E94;&#x7528;&#x6E32;&#x67D3;&#x57FA;&#x5EA7;&#x6807;&#x8BC6;</span>
<span class="hljs-comment">// activeRule: &#x5FAE;&#x5E94;&#x7528;&#x5165;&#x53E3;</span>
<span class="hljs-comment">// props: &#x5FAE;&#x5E94;&#x7528;&#x52A0;&#x8F7D;&#x4F20;&#x53C2;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> MICRO_APPS = {
    PersonalApp: {
        appName: <span class="hljs-string">&apos;&#x4E2A;&#x4EBA;&#x9879;&#x76EE;&apos;</span>,
        name: <span class="hljs-string">&apos;PersonalApp&apos;</span>,
        entry: <span class="hljs-string">&apos;//localhost:5173&apos;</span>,
        container: <span class="hljs-string">&apos;#app-vue3&apos;</span>,
        activeRule: <span class="hljs-string">&apos;/PersonalApp/index&apos;</span>,
        props: { brand: <span class="hljs-string">&apos;qiankun&apos;</span> },
    },
    IndustryApp: {
        appName: <span class="hljs-string">&apos;&#x884C;&#x4E1A;&#x9879;&#x76EE;&apos;</span>,
        name: <span class="hljs-string">&apos;IndustryApp&apos;</span>,
        entry: <span class="hljs-string">&apos;//localhost:5174&apos;</span>,
        container: <span class="hljs-string">&apos;#app-vue3&apos;</span>,
        activeRule: <span class="hljs-string">&apos;/IndustryApp/index&apos;</span>,
        props: { brand: <span class="hljs-string">&apos;qiankun&apos;</span> },
    },
};
<span class="hljs-comment">// &#x4F9D;&#x636E;-&#x5FAE;&#x5E94;&#x7528;&#x914D;&#x7F6E;&#x5BF9;&#x8C61;-&#x6CE8;&#x518C;&#x57FA;&#x5EA7;&#x8F6C;&#x53D1;&#x8DEF;&#x7531;&#x8868;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> registerMicroAppRouters = (router) =&gt; {
    <span class="hljs-keyword">const</span> buildRoute = (config) =&gt; {
        <span class="hljs-keyword">return</span> {
            path: <span class="hljs-string">`/<span class="hljs-subst">${config.name}</span>/:pathMatch(.*)?`</span>,
            name: <span class="hljs-string">`<span class="hljs-subst">${config.name}</span>`</span>,
            component: () =&gt; <span class="hljs-keyword">import</span>(<span class="hljs-string">&apos;../views/qiankunEntry.vue&apos;</span>),
            hidden: <span class="hljs-literal">true</span>,
            meta: {
                title: <span class="hljs-string">`<span class="hljs-subst">${config.appName}</span>`</span>,
            },
        };
    };
    <span class="hljs-keyword">const</span> route = {
        path: <span class="hljs-string">&apos;&apos;</span>,
        component: Layout,
        redirect: <span class="hljs-string">&apos;/index&apos;</span>,
        children: <span class="hljs-built_in">Object</span>.keys(MICRO_APPS).map(app =&gt; buildRoute(MICRO_APPS[app])),
    };
    router.addRoute(route);
};
</code></pre>
<p>&#x5199;&#x5165;&#x540E;&#x4E8E; <code>main.js</code> &#x4E2D;&#x5F15;&#x5165;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { registerMicroAppRouters } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./microApps&apos;</span>;

registerMicroAppRouters(router);
</code></pre>
<h3 id="&#x5B50;&#x5E94;&#x7528;&#x642D;&#x5EFA;&#xFF08;vite&#xFF09;"><a name="&#x5B50;&#x5E94;&#x7528;&#x642D;&#x5EFA;&#xFF08;vite&#xFF09;" class="anchor-navigation-ex-anchor" href="#&#x5B50;&#x5E94;&#x7528;&#x642D;&#x5EFA;&#xFF08;vite&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5B50;&#x5E94;&#x7528;&#x642D;&#x5EFA;&#xFF08;vite&#xFF09;</h3>
<h4 id="&#x5B89;&#x88C5;"><a name="&#x5B89;&#x88C5;" class="anchor-navigation-ex-anchor" href="#&#x5B89;&#x88C5;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5B89;&#x88C5;</h4>
<pre><code class="lang-bash">$ npm i vite-plugin-qiankun --save
</code></pre>
<p>&#x5B89;&#x88C5;&#x5B8C;&#x6210;&#x540E;&#x4E8E; <code>vite.config.js</code> &#x8FDB;&#x884C;&#x914D;&#x7F6E;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> qiankun <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;vite-plugin-qiankun&apos;</span>;
<span class="hljs-keyword">import</span> {name} <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./package.json&apos;</span>;
<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> defineConfig({
    resolve: {
        alias: {
            <span class="hljs-comment">// &#x8BBE;&#x7F6E;&#x8DEF;&#x5F84;</span>
            <span class="hljs-string">&apos;~&apos;</span>: path.resolve(__dirname, <span class="hljs-string">&apos;./&apos;</span>),
            <span class="hljs-comment">// &#x8BBE;&#x7F6E;&#x522B;&#x540D;</span>
            <span class="hljs-string">&apos;@&apos;</span>: path.resolve(__dirname, <span class="hljs-string">&apos;./src&apos;</span>),
        },
        extensions: [<span class="hljs-string">&apos;.mjs&apos;</span>, <span class="hljs-string">&apos;.js&apos;</span>, <span class="hljs-string">&apos;.ts&apos;</span>, <span class="hljs-string">&apos;.jsx&apos;</span>, <span class="hljs-string">&apos;.tsx&apos;</span>, <span class="hljs-string">&apos;.json&apos;</span>, <span class="hljs-string">&apos;.vue&apos;</span>],
    },
    plugins: [
        vue(),
        qiankun(<span class="hljs-string">`<span class="hljs-subst">${name}</span>`</span>, {useDevMode: <span class="hljs-literal">true</span>}),<span class="hljs-comment">// &#x914D;&#x7F6E; qiankun</span>
    ],
    server: {
        port: <span class="hljs-number">5173</span>,
        host: <span class="hljs-literal">true</span>,
        origin: <span class="hljs-string">&apos;//localhost:5173&apos;</span>,<span class="hljs-comment">// &#x914D;&#x7F6E; origin, &#x4F7F;&#x5FAE;&#x9879;&#x76EE;&#x65B9;&#x5F0F;&#x8BBF;&#x95EE;&#x65F6;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x53EF;&#x8BBF;&#x95EE;</span>
        proxy: {
            <span class="hljs-string">&apos;/dev-api&apos;</span>: {
                target: <span class="hljs-string">&apos;http://10.8.7.118:8080&apos;</span>,
                changeOrigin: <span class="hljs-literal">true</span>,
                rewrite: (p) =&gt; p.replace(<span class="hljs-regexp">/^\/dev-api/</span>, <span class="hljs-string">&apos;&apos;</span>),
            },
        },
    }
})
</code></pre>
<p><code>main.js</code> &#x4E2D;&#x6DFB;&#x52A0;&#x914D;&#x7F6E;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { createApp } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;vue&apos;</span>;
<span class="hljs-keyword">import</span> App <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./App.vue&apos;</span>;
<span class="hljs-keyword">import</span> { initRouter, unmountRouter } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./router&apos;</span>;

<span class="hljs-keyword">import</span> { renderWithQiankun, qiankunWindow } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;vite-plugin-qiankun/dist/helper&apos;</span>;

<span class="hljs-keyword">import</span> CgUI <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;cg-ui&apos;</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;cg-ui/lib/index.css&apos;</span>;

<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;./styles/index.css&apos;</span>;

<span class="hljs-keyword">import</span> { getToken } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;@/utils/auth&apos;</span>;
<span class="hljs-keyword">import</span> { useConfig } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./hooks/useConfig&apos;</span>;
<span class="hljs-keyword">import</span> pinia <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;@/pinia/index&apos;</span>;

<span class="hljs-keyword">let</span> instance = <span class="hljs-literal">null</span>;
<span class="hljs-keyword">let</span> container = <span class="hljs-literal">null</span>;

<span class="hljs-comment">/**
 * &#x6E32;&#x67D3;&#x5E94;&#x7528;
 * @param props
 */</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">render</span> (<span class="hljs-params">props = {}</span>) </span>{
    <span class="hljs-comment">// const { container } = props;</span>
    container = props.container;
    <span class="hljs-built_in">console</span>.log({ container });
    useConfig().updateMicroProps(props);
    instance = createApp(App);
    initRouter(props, instance);
    instance.use(pinia);
    instance.use(CgUI, {
        baseURL: <span class="hljs-keyword">import</span>.meta.env.VITE_APP_BASE_API,
        logExpired () {
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x767B;&#x5F55;&#x8FC7;&#x671F;&apos;</span>);
        },
        getToken,
        elementPlusConfig: {
            size: <span class="hljs-string">&apos;default&apos;</span>,
            zIndex: <span class="hljs-number">1000</span>,
        }
    });
    instance.mount(container ? container.querySelector(<span class="hljs-string">&apos;#app&apos;</span>) : <span class="hljs-string">&apos;#app&apos;</span>);
}
<span class="hljs-keyword">if</span> (!qiankunWindow.__POWERED_BY_QIANKUN__) {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x72EC;&#x7ACB;&#x5E94;&#x7528;&#x8FD0;&#x884C;&apos;</span>);
    useConfig().updateMicroModel(<span class="hljs-literal">false</span>);
    render({});
} <span class="hljs-keyword">else</span> {
    useConfig().updateMicroModel(<span class="hljs-literal">true</span>);
}
renderWithQiankun({
    <span class="hljs-comment">// &#x751F;&#x547D;&#x5468;&#x671F; - &#x6302;&#x8F7D;&#x540E;</span>
    <span class="hljs-keyword">async</span> mount (props) {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;dfcg-micro-front-personal -- mount&apos;</span>, props);
        render(props);
    },
    <span class="hljs-comment">// &#x751F;&#x547D;&#x5468;&#x671F; - &#x6302;&#x8F7D;&#x524D;</span>
    <span class="hljs-keyword">async</span> bootstrap () {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;dfcg-micro-front-personal -- bootstrap&apos;</span>);
    },
    <span class="hljs-keyword">async</span> update (props) {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;dfcg-micro-front-personal -- update&apos;</span>, props, instance);
        instance.mount(container ? container.querySelector(<span class="hljs-string">&apos;#app&apos;</span>) : <span class="hljs-string">&apos;#app&apos;</span>);
    },
    <span class="hljs-comment">// &#x751F;&#x547D;&#x5468;&#x671F; - &#x89E3;&#x9664;&#x6302;&#x8F7D;</span>
    <span class="hljs-keyword">async</span> unmount (props) {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;dfcg-micro-front-personal -- unmount&apos;</span>, props);
        instance.unmount();
        instance._container.innerHTML = <span class="hljs-string">&apos;&apos;</span>;
        instance = <span class="hljs-literal">null</span>;
        unmountRouter();
    },
});
</code></pre>
<p>&#x5B50;&#x5E94;&#x7528;&#x95F4;/&#x5B50;&#x4E3B;&#x95F4;&#x9875;&#x9762;&#x8DF3;&#x8F6C;</p>
<pre><code class="lang-javascript">history.pushState({}, <span class="hljs-string">&apos;&apos;</span>, <span class="hljs-string">&apos;/PersonalApp/about&apos;</span>);
</code></pre>
<p>&#x5B50;&#x5E94;&#x7528;&#x95F4;/&#x5B50;&#x4E3B;&#x95F4;&#x540C;&#x6B65;&#x6570;&#x636E;</p>
<pre><code class="lang-javascript">microProps.setGlobalState({test: <span class="hljs-string">&apos;2&apos;</span>, test1: {a: <span class="hljs-number">2</span>}})
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright &amp; copy zengqing.top 2023 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x66F4;&#x65B0;&#x4E8E;&#xFF1A;
2022-08-29 17:07:09
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../../Vue3/Vue3探索.html" class="navigation navigation-prev " aria-label="Previous page: 初识 Vue3">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../../Team/Web前端开发规范.html" class="navigation navigation-next " aria-label="Next page: 开发规范">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"qiankun","level":"8.1","depth":1,"next":{"title":"开发规范","level":"9.1","depth":1,"path":"Team/Web前端开发规范.md","ref":"./Team/Web前端开发规范.md","articles":[]},"previous":{"title":"初识 Vue3","level":"7.6","depth":1,"path":"Vue3/Vue3探索.md","ref":"./Vue3/Vue3探索.md","articles":[]},"dir":"ltr"},"config":{"plugins":["expandable-chapters","code","pageview-count","chapter-fold","expandable-chapters-small","tbfed-pagefooter","hide-element","ancre-navigation"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright & copy zengqing.top 2023","modify_label":"该文件更新于：","modify_format":"YYYY-MM-DD HH:mm:ss"},"chapter-fold":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"code":{"copyButtons":true},"hide-element":{"elements":[".gitbook-link"]},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"pageview-count":{},"ancre-navigation":{"showLevel":true,"associatedWithSummary":true,"mode":"float","printLog":false,"multipleH1":true,"float":{"showLevelIcon":false,"level1Icon":"","level2Icon":"","level3Icon":""},"pageTop":{"showLevelIcon":false,"level1Icon":"","level2Icon":"","level3Icon":""}},"expandable-chapters-small":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"XiaoChen","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"《前端学习笔记》","language":"zh-hans","gitbook":"*","description":"个人学习笔记，记录学习过程中的点点滴滴"},"file":{"path":"MicroApp/qiankun/index.md","mtime":"2022-08-29T09:07:09.676Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2023-03-26T04:06:26.355Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

